<template>
  <el-container>
    <!-- Header -->
    <el-header class="header">
      <div class="header-content">培训计划</div>
    </el-header>

    <!-- Main content -->
    <el-main>
      <div v-if="trainingPlans.length > 0" class="training-plan-list">
        <el-card
            v-for="plan in trainingPlans"
            :key="plan.id"
            class="training-plan-card"
            shadow="hover"
            :body-style="{ padding: '20px' }">

          <!-- 封面图片 -->
          <div class="plan-image">
            <img :src="plan.img" alt="培训计划封面" class="plan-img"/>
          </div>

          <div class="training-plan-details">
            <div class="plan-name">{{ plan.name }}</div>
            <div class="plan-description">{{ plan.description }}</div>
            <div class="plan-dates">
              <span>开始时间: {{ plan.startDate }}</span>
              <span>结束时间: {{ plan.endDate }}</span>
            </div>
            <div class="plan-action">
              <el-button
                  :disabled="isPlanExpired(plan.endDate)"
                  type="primary"
                  class="join-btn"
                  @click="joinTrainingPlan(plan.id)">
                加入我的课程
              </el-button>
              <!-- 显示“已过期”文字 -->
              <span v-if="isPlanExpired(plan.endDate)" class="expired-text">已过期</span>
            </div>
          </div>
        </el-card>
      </div>
      <div v-else>
        <p class="no-training-plans">暂无培训计划。</p>
      </div>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from '@/utiles/axios';
import { ElMessage } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';

const trainingPlans = ref([]);
const router = useRouter();
const route = useRoute();

// 获取当前机构的培训计划
const getTrainingPlans = async () => {
  const organizationId = localStorage.getItem('organizationId');  // 从本地存储获取机构ID
  if (!organizationId) {
    ElMessage.error('无法获取机构信息');
    return;
  }

  try {
    const response = await axios.get(`/trainingPlan/${organizationId}`);
    trainingPlans.value = response.data;
  } catch (error) {
    console.error('获取培训计划失败', error);
    ElMessage.error('获取培训计划失败');
  }
};

// 判断培训计划是否已过期
const isPlanExpired = (endDate) => {
  const currentDate = new Date();
  const end = new Date(endDate);
  return currentDate > end;  // 如果当前日期大于截止日期，返回 true
};

// 学员加入培训计划
const joinTrainingPlan = async (trainingPlanId) => {
  const studentId = localStorage.getItem('studentId');  // 从本地存储获取学员ID
  if (!studentId) {
    ElMessage.error('无法获取学员信息');
    return;
  }

  // 检查是否已有培训计划
  try {
    const studentResponse = await axios.get(`/student/info?id=${studentId}`);
    if (studentResponse.data.trainingPlan) {
      ElMessage.warning('您已经加入了一个培训计划，无法重复选择');
      return;
    }

    // 加入培训计划
    const response = await axios.put(`/student/${studentId}/join/${trainingPlanId}`);
    ElMessage.success('加入培训计划成功');
    await getTrainingPlans();  // 刷新培训计划
  } catch (error) {
    console.error('加入培训计划失败', error);
    ElMessage.error('加入培训计划失败');
  }
};

onMounted(() => {
  getTrainingPlans();  // 页面加载时获取培训计划
});
</script>

<style scoped>
/* 背景色、字体和内边距 */
.el-header {
  background-color: #409EFF;
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  padding: 15px;
}

.header-content {
  padding: 10px 0;
}

/* 训练计划列表样式 */
.training-plan-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  padding: 20px;
}

.training-plan-card {
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border-radius: 12px;
  overflow: hidden;
}

.training-plan-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

/* 培训计划封面图片 */
.plan-image {
  width: 100%;
  height: 200px;
  overflow: hidden;
  border-radius: 8px;
}

.plan-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 培训计划详情 */
.training-plan-details {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.plan-name {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.plan-description {
  font-size: 14px;
  color: #666;
}

.plan-dates {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}

/* 加入按钮和已过期文字 */
.plan-action {
  display: flex;
  align-items: center;
  gap: 10px;
}

.join-btn {
  margin-top: 10px;
  align-self: flex-start;
  background-color: #409EFF;
  border-color: #409EFF;
  padding: 10px 20px;
  font-size: 14px;
}

.join-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 已过期文字样式 */
.expired-text {
  font-size: 14px;
  color: #ff4d4f;
  font-weight: bold;
}

/* 空培训计划时的提示 */
.no-training-plans {
  text-align: center;
  font-size: 18px;
  color: #999;
}
</style>
